<template>
	<section>
		<!--工具条-->
	<el-card>
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="marketAdd">新增</el-button>
				</el-form-item>
				<el-form-item style="float:right">
					<el-button type="primary" v-on:click="getList">查询</el-button>
				</el-form-item>
				<el-form-item style="float:right">
					<el-input v-model="filters.marketName" placeholder="市场名称"></el-input>
				</el-form-item>
			</el-form>
		</el-col>
	</el-card>
		<!--列表-->
		<el-table :data="tableData.slice((page-1)*pagesize,page*pagesize)" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" label="序号" width="60">
			</el-table-column>
			<el-table-column prop="marketName" label="市场名称" width="100" sortable>
			</el-table-column>
			<el-table-column prop="adminArea" label="行政区域" width="180" sortable>
			</el-table-column>
			<el-table-column prop="marketType" label="环境类型" width="120" sortable>
			</el-table-column>
			<el-table-column prop="companyCode" label="企业编码" min-width="120" sortable>
            </el-table-column>
			<el-table-column prop="synchroUrl" label="同步URL地址" min-width="180" sortable>
            </el-table-column>
			<el-table-column prop="isSynchro" label="是否启用同步" min-width="120" :formatter="formatIsSynchro" sortable>
            </el-table-column>
			<el-table-column prop="isEnable" label="是否有效" min-width="120" :formatter="formatIsEnable" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="marketEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="MarketDelete(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="page" :page-size="pagesize" :total="tableData.length" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :modal-append-to-body="false">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="市场名称" prop="marketName">
					<el-input v-model="editForm.marketName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="行政区域">
					<el-input v-model="editForm.adminArea"></el-input>
				</el-form-item>
				<el-form-item label="环境类型">
					<el-select v-model="editForm.marketType" placeholder="环境类型">
						<el-option label="正式环境" value="正式环境"></el-option>
						<el-option label="测试环境" value="测试环境"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="企业编码" prop="companyCode">
					<el-input v-model="editForm.companyCode"></el-input>
				</el-form-item>
				<el-form-item label="同步URL地址" prop="synchroUrl">
					<el-input v-model="editForm.synchroUrl"></el-input>
				</el-form-item>
				<el-form-item label="是否启用同步">
					<el-radio-group v-model="editForm.isSynchro">
						<el-radio class="radio" :label="1">是</el-radio>
						<el-radio class="radio" :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="是否有效">
					<el-radio-group v-model="editForm.isEnable">
						<el-radio class="radio" :label="1">是</el-radio>
						<el-radio class="radio" :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :modal-append-to-body="false">
			<el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
				<el-form-item label="市场名称" prop="marketName">
					<el-input v-model="addForm.marketName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="行政区域">
					<el-input v-model="addForm.adminArea"></el-input>
				</el-form-item>
				<el-form-item label="环境类型">
					<el-select v-model="addForm.marketType" placeholder="环境类型">
						<el-option label="正式环境" value="正式环境"></el-option>
						<el-option label="测试环境" value="测试环境"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="企业编码" prop="companyCode">
					<el-input v-model="addForm.companyCode"></el-input>
				</el-form-item>
				<el-form-item label="同步URL地址" prop="synchroUrl">
					<el-input v-model="addForm.synchroUrl"></el-input>
				</el-form-item>
				<el-form-item label="是否启用同步">
					<el-radio-group v-model="addForm.isSynchro">
						<el-radio class="radio" :label="1">是</el-radio>
						<el-radio class="radio" :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="是否有效">
					<el-radio-group v-model="addForm.isEnable">
						<el-radio class="radio" :label="1">是</el-radio>
						<el-radio class="radio" :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	// import util from '../api/util'
	//import NProgress from 'nprogress'
	import { getMarketList,addMarket,editMarket,deleteMarket } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					marketNmae: ''
				},
				tableData: [],
				total: 0,
				page: 1,
				pagesize:2,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					marketName: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					companyCode: [
						{ required: true, message: '请输入企业编码', trigger: 'blur' }
					],
					synchroUrl: [
						{ required: true, message: '请输入同步URL地址', trigger: 'blur' }
					],
				},
				//编辑界面数据
				editForm: {
					marketCode: 0,
					marketName: '',
					adminArea: '下拉框',
					marketType: '测试环境',
					companyCode: '',
					synchroUrl: '',
					isSynchro: -1,
					isEnable: -1
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					marketName: [
						{ required: true, message: '请输入市场名称', trigger: 'blur' }
					],
					companyCode: [
						{ required: true, message: '请输入企业编码', trigger: 'blur' }
					],
					synchroUrl: [
						{ required: true, message: '请输入同步URL地址', trigger: 'blur' }
					],
				},
				//新增界面数据
				addForm: {
					marketName:'',
					adminArea: '下拉框',
					marketType: '测试环境',
					companyCode: '',
					synchroUrl: '',
					isSynchro: 1,
					isEnable: -1
				}
			}
		},
		methods: {
			//是否启用同步显示转换
			formatIsSynchro: function (row, column) {
				return row.isSynchro == 1 ? '是' : row.isSynchro == 0 ? '否' : '未知';
			},
			formatIsEnable: function (row, column) {
				return row.isEnable == 1 ? '是' : row.isEnable == 0 ? '否' : '未知';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getList();
			},
			//获取列表
			getList() {
				let params = {
					page: this.page,
					marketName: this.filters.marketName
				};
				this.listLoading = true;
				//NProgress.start();
				getMarketList(params).then(function(result){
					this.tableData = result.data;
					this.total = result.data.length;
					this.listLoading = false;
				}.bind(this)).catch(function (error) {
					this.listLoading = false;
					console.log(error);
				}.bind(this));
			},
			//删除
			MarketDelete: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = {marketCode:row.marketCode};
					deleteMarket(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getList();
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					}); 
				});
			},
			//显示编辑界面
			marketEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			marketAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					marketName: '',
					adminArea: '下拉框',
					marketType: '测试环境',
					companyCode: '',
					synchroUrl: '',
					isSynchro: 1,
					isEnable: -1
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.editForm);
							editMarket(params).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getList();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.addForm);
							addMarket(params).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getList();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getList();
		}
	}

</script>